<%@page import="tu.java.utils.UIUtils"%>
<%@page import="tu.java.login.User.accountType"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="tu.java.login.User"%>
<%	
	User user = (User) session.getAttribute("userObj");
    if( request.getMethod().equals("POST"))
    {
       	String register = request.getParameter("register");
    	if(register!=null && register.equals("yes")){
	    	User userNew = new User();
	    	userNew.setEmail(request.getParameter("email"));
	    	userNew.setPassword(request.getParameter("hp"));
	    	userNew.setFirstName(request.getParameter("firstName"));
	    	userNew.setLastName(request.getParameter("lastName"));
			String userType = request.getParameter("userType");
			if(userType!=null){
				userNew.setType(user.getType(Integer.parseInt(userType)));
			}
			else{
				userNew.setType(accountType.User);
			}
			
			if( userNew.RegisterUser()){
				response.sendRedirect(request.getContextPath());
			}
			else{
				user= new User();
			%>
			<script type="text/javascript">
			
			alert("Потребителят вече съществува");
			window.location.href='<%=request.getContextPath()%>';
			</script>
			<% }
    	}		
    }    
    %>
    <script type="text/javascript" src="<%=request.getContextPath()%>/js/sha512.js"></script> 
<script type="text/javascript" src="<%=request.getContextPath()%>/js/forms.js"></script> 
 <div id="content">
		  <div id="registration-box" class="login-popup">
        <a href="<%=request.getRequestURI()%>" class="close" type="reset"><img src="<%=request.getContextPath()%>/img/close_pop.png" class="btn_close" title="Close Window" alt="Close" /></a>
		<form class="signin"  name="registerform" method="post" action="<%=request.getContextPath()%>/login/registration.jsp">
		<input type="hidden" name="register" value="">
		<fieldset class="textbox">
			<label for="firstName">
                <span>Име:</span>
                <input id="firstName" name="firstName" value="" type="text" autocomplete="on" placeholder="Име"  onblur="validateUsername(this.form.firstName)">
            </label>
            
            <label for="lastName">
                <span>Фамилия:</span>
                <input id="lastName" name="lastName" value="" type="text" autocomplete="on" placeholder="Фамилия" onblur="validateUsername(this.form.lastName)">
            </label>
            
            <label for="email">
                <span>Е-поща:</span>
                <input id="email" name="email" value="" type="text" autocomplete="on" placeholder="Е-поща"  onblur="validateEmail(this.form.email)">
            </label>
            
            <label for="password">
                <span>Парола:</span>
                <input id="password" name="password" value="" type="password" autocomplete="on" placeholder="Парола" onblur="validatePassword(this.form.password)">
            </label>
            <%if(user.getType().equals(accountType.Admin)){%>
            <label for="userType">
                <span>Вид потребител:</span>
               	<select name="userType">
               		<%=UIUtils.printOptions("SELECT CS01_NOMENCLATURE_ID,CS01_DESCRIPTION FROM TS01_NOMENCLATURES WHERE CS01_NOMENCLATURE_ID_FROM='100'", 0) %>               	
               	</select>
            </label>
            <%}%>
             <button type="button" class="button" onclick="javascript:document.forms.registerform.register.value='yes'; formhash(this.form, this.form.password)">Регистрация</button>
		</fieldset> 
		</form>
		</div>
		</div>